(function(){BX.namespace("BX.Main.User");if(BX.Main.User.Selector){return}function e(e){this.caller=e.caller;this.container=BX(e.containerId);this.id=e.id;this.containerId=e.containerId;this.inputName=e.inputName;this.inputId=e.inputName;this.isInputMultiple=e.isInputMultiple;this.inputNode=this.container?this.container.querySelector('input[name="'+e.inputName+'"]'):null;this.useSymbolicId=e.useSymbolicId;this.openDialogWhenInit=!!e.openDialogWhenInit;this.selector=BX.UI.TileSelector.getById(this.id);if(!this.selector){throw new Error("Tile selector `"+this.id+"` not found.")}this.searchInputNode=this.selector.getSearchInput();if(!this.searchInputNode.id){this.searchInputNode.id=this.inputId+"-"+this.id+"-search-input"}this.lazyload=!!e.lazyload;BX.addCustomEvent(this.selector,this.selector.events.buttonSelect,this.openDialog.bind(this));BX.addCustomEvent(this.selector,this.selector.events.tileRemove,this.removeTile.bind(this));BX.addCustomEvent(this.selector,this.selector.events.tileClick,this.clickTile.bind(this));BX.Main.User.SelectorController.init(this)}e.prototype={openDialog:function(){if(this.lazyload){var e=false;if(BX.Main.selectorManagerV2){var t=BX.Main.selectorManagerV2.getById(this.id);if(t&&t.initialized){BX.Main.User.SelectorController.open(this);e=true}}if(!e){BX.onCustomEvent("BX.Main.SelectorV2:initDialog",[{selectorId:this.id,openDialogWhenInit:true}])}}else{BX.Main.User.SelectorController.open(this)}},removeTile:function(e){this.unsetValue(e.id)},clickTile:function(e){if(BX.type.isNotEmptyObject(e.data)&&BX.type.isNotEmptyString(e.data.url)){if(BX.type.isNotEmptyString(e.data.urlUseSlider)&&e.data.urlUseSlider=="Y"&&BX.type.isNotEmptyObject(BX.SidePanel)){BX.SidePanel.Instance.open(e.data.url)}else{window.open(e.data.url,"_blank")}}},setUsers:function(e){e=e||[];if(this.isInputMultiple){this.addInputs(e)}else{this.inputNode.value=e.join(",");BX.fireEvent(this.inputNode,"change")}},getUsers:function(){if(!this.isInputMultiple&&!this.inputNode){return[]}var e;if(this.isInputMultiple){e=this.getInputs().map((function(e){return e.value}))}else{e=this.inputNode.value.split(",")}if(!this.useSymbolicId){return e.filter((function(e){e=parseInt(e);return!!e})).map((function(e){return parseInt(e)}))}else{return e.filter((function(e){return e.length>0}))}},setValue:function(e){if(!this.useSymbolicId){if(/^\d+$/.test(e)!==true){return}e=parseInt(e)}if(this.selectOne){this.setUsers([e])}else{var t=this.getUsers();if(!BX.util.in_array(e,t)){t.push(e)}this.setUsers(t)}},unsetValue:function(e){if(!this.useSymbolicId){if(/^\d+$/.test(e)!==true){return}e=parseInt(e)}if(this.selectOne){this.setUsers()}else{var t=false;if(BX.type.isNotEmptyObject(BX.Main.selectorManagerV2)){var i=BX.Main.selectorManagerV2.getById(this.id);if(i.getOption("returnJsonValue")=="Y"){t=true}}var n=this.getUsers().filter((function(i){if(t){var n=JSON.parse(i);if(BX.type.isNotEmptyObject(n)){i=n.id}}return i!==e}));this.setUsers(n)}},addInput:function(e){var t=document.createElement("input");t.type="hidden";t.name=this.inputName;t.value=e;this.container.appendChild(t);BX.fireEvent(t,"change");BX.Event.EventEmitter.emit(window,"BX.Main.User.SelectorController:itemRendered",[{selectorId:this.id,value:e}])},addInputs:function(e){this.removeInputs();e.forEach((function(e){this.addInput(e)}),this);if(e.length<=0&&this.isInputMultiple){this.addInput("")}},getInputs:function(){return BX.convert.nodeListToArray(this.container.querySelectorAll('input[name="'+this.inputName+'"]'))},removeInputs:function(){this.getInputs().forEach((function(e){BX.fireEvent(e,"change");BX.remove(e)}))}};var t={list:[],init:function(e){this.list.push(e);BX.onCustomEvent(window,"BX.Main.User.SelectorController::init",[{id:e.id,inputId:e.searchInputNode.id,containerId:e.containerId,openDialogWhenInit:e.openDialogWhenInit}])},open:function(e){if(e.isOpen){return}if(BX.UI.SelectorManager){var t=BX.UI.SelectorManager.instances[e.id];if(t){if(!e.isInputMultiple){t.itemsSelected={}}e.getUsers().forEach((function(e){var i=null;for(var n in t.entities){if(t.entities.hasOwnProperty(n)&&BX.type.isNotEmptyObject(t.entities[n].items)){if(BX.util.in_array(e,Object.keys(t.entities[n].items))){i=n}}}if(i){t.itemsSelected[e]=i.toLowerCase()}}));t.nodes.input=e.selector.input;t.nodes.tag=e.selector.buttonSelect}}e.isOpen=true;BX.onCustomEvent(window,"BX.Main.User.SelectorController::open",[{id:e.id,inputId:e.searchInputNode.id,containerId:e.containerId,bindNode:e.container}])},formatName:function(e){var t="";var i=BX.type.isNotEmptyString(e.nameTemplate)?BX.util.htmlspecialcharsback(BX.util.htmlspecialcharsback(e.nameTemplate)):"#NAME#";if(BX.type.isNotEmptyObject(e.item)){var n=e.item;t=i}else{return t}for(var r in n){if(n.hasOwnProperty(r)){t=t.replace("#"+r.toUpperCase()+"#",BX.util.htmlspecialcharsback(n[r]))}}return t},select:function(e){var t=BX.type.isNotEmptyObject(BX.Main.selectorManagerV2)?BX.Main.selectorManagerV2.getById(e.selectorId):null;var i=BX.Main.User.SelectorController;var n=i.getUserSelector(e.selectorId);if(!n||!BX.type.isNotEmptyObject(e.item)){return}var r=n.useSymbolicId?e.item.id:e.item.entityId;if(BX.type.isNotEmptyObject(e.item.params)&&BX.type.isNotEmptyString(e.item.params.email)&&(!t||t.getOption("returnJsonValue")!="Y")){r=(BX.type.isNotEmptyString(e.prefix)?e.prefix:"UE")+r}var s=false;if(t&&t.getOption("returnJsonValue")=="Y"){n.setValue(JSON.stringify(e.item))}else{n.setValue(r)}var o={readonly:!!e.undeletable};if(BX.type.isNotEmptyString(e.entityType)){o.entityType=e.entityType}if(BX.type.isNotEmptyString(e.item.url)){o.url=e.item.url}if(BX.type.isNotEmptyString(e.item.urlUseSlider)){o.urlUseSlider=e.item.urlUseSlider}if(BX.type.isNotEmptyString(e.item.isExtranet)&&e.item.isExtranet=="Y"){o.extranet=true}if(BX.type.isNotEmptyString(e.item.isCrmEmail)&&e.item.isCrmEmail=="Y"){o.crmEmail=true}if(BX.type.isNotEmptyString(e.state)){o.state=e.state}n.selector.addTile(i.formatName({item:e.item,nameTemplate:t?t.getOption("nameTemplate"):"#NAME# #LAST_NAME#"}),o,r);n.selector.input.value="";if(!n.isInputMultiple||!BX.type.isNotEmptyString(e.tab)||e.tab!="search"){n.selector.input.style.display="none";n.selector.buttonSelect.style.display=""}BX.onCustomEvent("BX.Main.User.SelectorController:select",[{selectorId:e.selectorId,item:e.item,contextNode:n.selector.context,containerId:n.containerId,inputName:n.inputName}])},unSelect:function(e){var t=BX.Main.User.SelectorController;var i=t.getUserSelector(e.selectorId);if(!i||!BX.type.isNotEmptyObject(e.item)){return}var n=i.useSymbolicId?e.item.id:e.item.entityId;i.unsetValue(n);var r=i.selector.getTile(n);if(r){i.selector.removeTile(r)}if(BX.UI.SelectorManager){var s=BX.UI.SelectorManager.instances[e.selectorId];if(s){if(typeof s.deleteSelectedItem=="function"){s.deleteSelectedItem({itemId:e.item.id})}else{delete s.itemsSelected[e.item.id]}}}if(!i.isInputMultiple||!BX.type.isNotEmptyString(e.tab)||e.tab!="search"){i.selector.input.style.display="none";i.selector.buttonSelect.style.display=""}BX.onCustomEvent("BX.Main.User.SelectorController:unSelect",[{selectorId:e.selectorId,item:e.item,contextNode:i.selector.context,containerId:i.containerId,inputName:i.inputName}])},openDialog:function(e){var t=BX.Main.User.SelectorController;var i=t.getUserSelector(e.selectorId);if(!i){return}i.isOpen=true;if(i.selector){i.selector.input.style.display="";i.selector.buttonSelect.style.display="none";i.selector.input.focus()}},closeDialog:function(e){var t=BX.Main.User.SelectorController;var i=t.getUserSelector(e.selectorId);if(!i){return}i.isOpen=false;if(i.selector){i.selector.input.style.display="none";i.selector.buttonSelect.style.display=""}},openSearch:function(e){var t=BX.Main.User.SelectorController;var i=t.getUserSelector(e.selectorId);if(!i){return}i.isOpen=false;if(i.selector){i.selector.input.style.display="";i.selector.buttonSelect.style.display="none"}},closeSearch:function(e){var t=BX.Main.User.SelectorController;var i=t.getUserSelector(e.selectorId);if(!i){return}if(i.selector){var n=BX.UI.SelectorManager.instances[e.selectorId];if(!n||!n.closeByEmptySearchResult){i.selector.input.style.display="none";i.selector.buttonSelect.style.display=""}}},getUserSelector:function(e){var t=this.list.filter((function(t){return t.id===e&&(!t.container||document.body.contains(t.container))}));return t[0]}};if(!BX.Main.User.SelectorController){BX.Main.User.SelectorController=t}BX.Main.User.Selector=e})(window);
//# sourceMappingURL=script.map.js